package com.ccsu.mymarket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccsu.mymarket.dao.ShoppingCartDao;
import com.ccsu.mymarket.entity.ShoppingCart;
import com.ccsu.mymarket.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {
    private ShoppingCartDao cartDao;

    public ShoppingCartServiceImpl(@Autowired ShoppingCartDao cartDao) {
        this.cartDao = cartDao;
    }

    @Override
    public boolean deleteCart(Integer type, Integer id) {
        switch (type){
//            按照购物车记录id删除
            case 1:
                if (this.removeById(id))
                    return true;
//                按照用户id删除
            case 0:
                cartDao.delete(new LambdaUpdateWrapper<ShoppingCart>().eq(ShoppingCart::getAccId,id));
                return true;
        }
        return false;
    }

    @Override
    public boolean updateCart(ShoppingCart cart) {
        Integer row= cartDao.update(cart, new LambdaUpdateWrapper<ShoppingCart>()
                .eq(ShoppingCart::getAccId,cart.getAccId())
                .eq(ShoppingCart::getBookId,cart.getBookId())
                .set(ShoppingCart::getNum,cart.getNum()));
        if (row>0)
            return true;
        return false;
    }
}
